home *** CD-ROM | disk | FTP | other *** search
/ Crack It! / Crack It!.iso / CONTENT / DISKEDIT / HEXFUNCS.PAS < prev    next >
Pascal/Delphi Source File  |  1996-09-09  |  854b  |  59 lines

  1. {
  2.  ***
  3.  
  4.  HEXFUNCS.PAS
  5.  Unit Implementing Decimal-Hexadecimal and Hexadecimal-Decimal Conversions
  6.  (C)Copyright Gerard Paul Java 1996
  7.  
  8.  ***
  9. }
  10.  
  11. unit HexFuncs;
  12.  
  13. interface
  14.  
  15. type
  16.   HexByte = string[2];
  17.  
  18. function ToHex(X: byte): HexByte;
  19. function FromHex(Digit: char): byte;
  20.  
  21. implementation
  22.  
  23. function ToHex(X: byte): HexByte;
  24. var
  25.   Digit1,
  26.   Digit2: byte;
  27.  
  28. function HexOneDigit(Y: byte): char;
  29. var
  30.   Result: byte;
  31.  
  32. begin
  33.   if Y < $A then
  34.     Result := Y
  35.   else
  36.     Result := Y+7;
  37.  
  38.   Inc(Result,48);
  39.  
  40.   HexOneDigit := Chr(Result);
  41. end;
  42.  
  43. begin
  44.   Digit1 := X mod 16;
  45.   Digit2 := X div 16;
  46.  
  47.   ToHex := HexOneDigit(Digit2)+HexOneDigit(Digit1);
  48. end;
  49.  
  50. function FromHex(Digit: char): byte;
  51. begin
  52.   case Digit of
  53.     '0'..'9': FromHex := Ord(Digit)-48;
  54.     'A'..'F': FromHex := Ord(Digit)-55;
  55.   end;
  56. end;
  57.  
  58. end.
  59.